package com.haima.wemadia.gateway.filter;

import com.alibaba.fastjson.JSON;
import com.haima.wemadia.gateway.util.AppJwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@Component//spring主键注解
@Slf4j
@Order(0)//过滤器执行循序
public class AuthorizeFilter implements GlobalFilter {
    //放行白名单
    private static ArrayList<String> allowUrls = new ArrayList<> ();

    static {
        allowUrls.add ( "/login/in" );//登录方法
        allowUrls.add("/v2/api-docs");
    }

    /**
     * @param exchange 封装请求和响应对象
     * @param chain    链对象
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //通过exchange获取请求对象
        ServerHttpRequest request = exchange.getRequest ();
        //检查路径是否需要拦截
        String path = request.getURI ().getPath ();//请求路径
        for (String allowUrl : allowUrls) {
            if (path.contains ( allowUrl )) {
                return chain.filter ( exchange );//请求通过
            }
        }
        //获取token信息
        //获取请求头
        String token = request.getHeaders ().getFirst ( "token" );
        //如果token不存在返回401
        if (StringUtils.isBlank ( token )) {
            return writeMessage ( exchange, "请登录后访问" );
        }
        //解析token
        //1,解析载荷信息
        try {
            Claims claimsBody = AppJwtUtil.getClaimsBody ( token );
            int i = AppJwtUtil.verifyToken ( claimsBody );
            if (i<1){
                //有效
                //解析成功获取token中存储的用户id，并将id写入到请求头中 路由给其他微服务
                Object id = claimsBody.get ( "id" );//获取登录id
                request.mutate ().header ( "userId",String.valueOf ( id ) );
                exchange.mutate ().request ( request );
                log.info("解析token成功  当前登录用户id ==> {}",id);
                return chain.filter ( exchange );
            }
            log.error ( "解析用户token失败 ==>token已过期" );
        } catch (Exception e) {
            e.printStackTrace ();
            log.error ( "解析用户token失败 ==>{}",e.getMessage () );
        }
        //解析失败终止请求，返回401
        return writeMessage ( exchange,"解析用户token失败，请重新登录" );
    }

    private Mono<Void> writeMessage(ServerWebExchange exchange, String msg) {
        Map map = new HashMap ();
        map.put ( "code", "401" );
        map.put ( "errorMessage", msg );
        //获取响应对象
        ServerHttpResponse response = exchange.getResponse ();
        //设置响应的状态码
        response.setStatusCode ( HttpStatus.UNAUTHORIZED );
        //设置响应的数据格式
        response.getHeaders ().setContentType ( MediaType.APPLICATION_JSON );
        //设置响应数据
        DataBuffer wrap = response.bufferFactory ().wrap ( JSON.toJSONBytes ( map ) );

        //响应数据
        return   response.writeWith ( Flux.just ( wrap ) );
    }
}
